/*
* @Author: Rosen
* @Date:   2017-05-22 09:08:57
* @Last Modified by:   Administrator
* @Last Modified time: 2017-11-22 14:31:32
*/

$(function(){

    $("footer").load("layout/footer.html");
    $("header").load("layout/header2.html");
//身份切换
    $(document).on("click" ,".regtitle",function(){

                $(this).addClass("borderred").siblings().removeClass("borderred");

                if($('.user-title1').hasClass("borderred")){
                     $(".user-box1 .zhizhaobox").hide()
                
                }else{
                    $(".user-box1 .zhizhaobox").show() 
                }
                   
    })
// 手机失去焦点
$(document).on("blur","#userphone",function(){
    var phone=$(this).val().replace(/^\s+|\s+$/g,'');
    if(phone==""){
         $(this).parents(".user-item").find(".tishi-msg").show().html("手机号不能为空")
    }else if(!(/^1[34578]\d{9}$/.test(phone))){
        
        $(this).parents(".user-item").find(".tishi-msg").show().html("您输入的手机号有误");
        
    }else{
    	$.ajax({
    		type:"post",
            url:"/SST/user/onlyphone.do",
            async:true,
            data: {"phone":phone},
            success: function(res){
            	if(res==1){
            		alert("该手机号码已经注册请重新输入");
            	}
            }
    		
    	});
    }
    
})
//手机获得焦点
$(document).on("focus","#userphone",function(){
    $(this).parents(".user-item").find(".tishi-msg").hide()
})
// 密码获取焦点
$(document).on("focus","#password",function(){
     var phone=$("#userphone").val().replace(/^\s+|\s+$/g,'');
     if(phone==""){
        $("#userphone").parents(".user-item").find(".tishi-msg").show().html("手机号不能为空")
     }else{
         $(this).parents(".user-item").find(".tishi-msg").show().html("建议使用字母、数字组合，6-20个字符")
     }
})
// 密码失去焦点
$(document).on("blur","#password",function(){
    var password=$("#password").val().replace(/^\s+|\s+$/g,'');
    if(password!=""){
        if(!(/^[A-Za-z0-9]{6,20}$/.test(password))){
          $(this).parents(".user-item").find(".tishi-msg").show().html("密码有误").css("color","red")
        }else{
           $(this).parents(".user-item").find(".tishi-msg").hide()
        }
    }
  })
// 确认密码失去焦点
$(document).on("blur","#passagain",function(){
    var password=$("#password").val().replace(/^\s+|\s+$/g,'');
    var passagain=$("#passagain").val().replace(/^\s+|\s+$/g,'');
    if(passagain!=""){
        if(passagain!=password){
          $(this).parents(".user-item").find(".tishi-msg").show().html("两次输入密码不一致")
        }else{
           $(this).parents(".user-item").find(".tishi-msg").hide()
           $("#password").parents(".user-item").find(".tishi-msg").hide()
        }
    }
  })
// 营业执照失去焦点
/*$(document).on("blur","#zhizhaohao",function(){
    var zhizhaohao=$("#zhizhaohao").val().replace(/^\s+|\s+$/g,'');
     if(zhizhaohao==""){
          $(this).parents(".user-item").find(".tishi-msg").show().html("营业执照号不能为空")
     }else{
        if(!(/(^\d{15}$)|(^\d{18}$)/.test(zhizhaohao))) {
          $(this).parents(".user-item").find(".tishi-msg").show().html("营业执照号格式错误")

        }else{
           $(this).parents(".user-item").find(".tishi-msg").hide()
        }
     }
  })*/
 /*防刷新：检测是否存在cookie*/  
        if($.cookie("captcha")){  
            var count = $.cookie("captcha");  
            var btn = $('#getting');  
            btn.val("重新获取 ("+count+") ").attr('disabled',true).css({
              'cursor':'not-allowed',
              'background':'#CCC',
            });  
            var resend = setInterval(function(){  
                count--;  
                if (count > 0){  
                    btn.val("重新获取 ("+count+") ").attr('disabled',true).css('cursor','not-allowed');  
                    $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});  
                }else {  
                    clearInterval(resend);  
                    btn.val("获取验证码").removeClass('disabled').removeAttr('disabled style');  
                }   
            }, 1000);  
        } 
// 
//获取验证码按钮
$(document).on("click",".yzm",function(){
       var  phone=$("#userphone").val();
       var  password=$("#password").val();
       var  passagain=$("#passagain").val();
       var  zhizhaohao=$("#zhizhaohao").val();
       var  yzm=$("#yzm").val();

        if(phone==""){
             $(this).parents(".user-item").find(".tishi-msg").show().html("手机号码不能为空") 
        }else if(!(/^1[34578]\d{9}$/.test(phone))){
            $(this).parents(".user-item").find(".tishi-msg").show().html("手机号码不正确")
        }else{
          if(password==""||!(/^[A-Za-z0-9]{6,20}$/.test(password))){
               $("#password").parents(".user-item").find(".tishi-msg").show().html("密码输入有误") 
            }else{
                if(passagain!=password){
                   $("#password").parents(".user-item").find(".tishi-msg").show().html("两次输入密码不一致")  
                }else{
                    if($('.user-title1').hasClass("borderred")){
                        $("#yzm").parents(".user-item").find(".tishi-msg").hide()
                           $.ajax({
                                          type:"post",
                                          url:"/SST/user/sendVerifyCode.do",
                                          async:true,
                                          data: {"phone":phone},
                                          success: function(res){
                                             if(res=true){
                                                alert("验证码已发送")
                                             }else{
                                            	 alert("验证码发送失败")
                                             }
                                          },
                                          error: function(){
                                            alert("请刷新重试")
                                          }
                         });
                         
                        var btn = $(this);  
                                        var count = 60;  
                                        var resend = setInterval(function(){  
                                                   count--;  
                                                   if (count > 0){  
                                                       btn.val("重新获取 ("+count+") ").css({
                                                         'cursor':'not-allowed',
                                                         'background':'#CCC'
                                                       });   
                                                       $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});  
                                                   }else {  
                                                       clearInterval(resend);  
                                                       btn.val("获取验证码").removeAttr('disabled style').css("background","red");  
                                                   }  
                                               }, 1000);  
                                               btn.attr('disabled',true).css('cursor','not-allowed'); 
                   }else{
                       if (zhizhaohao=="") {
                           $("#zhizhaohao").parents(".user-item").find(".tishi-msg").show().html("营业执照号不能为空")  
                        }else{
                           /*if(!(/(^\d{15}$)|(^\d{18}$)/.test(zhizhaohao))) {
                            $(this).parents(".user-item").find(".tishi-msg").show().html("营业执照号格式错误")

                          }else{*/
                             $(this).parents(".user-item").find(".tishi-msg").hide()
                         
                             
                              $.ajax({
                                          type:"post",
                                          url:"/SST/user/sendVerifyCode.do",
                                          async:true,
                                          data: {"phone":phone},
                                          success: function(res){
                                             if(res=true){
                                                alert("验证码已发送")
                                             }else{
                                            	 alert("验证码发送失败")
                                             }
                                          },
                                          error: function(){
                                            alert("请刷新重试")
                                          }
                         });
                              //     $.ajax({
                              //                type:"post",
                              //                url:"",
                              //                async:true,
                              //                data: {"phone":phone,"password":password,"passagain":passagain,"yzm":yzm},
                              //                success: function(res){
                              //                   if(res.num==2){
                              //                      alert("手机号已注册，请换号注册")
                              //                   }else if(res.num==0){
                              //                      alert("您已被加入黑名单，可以申请解禁")
                              //                   }else{

                              //                   }
                                                   
                              //                },
                              //                error: function(){
                              //                  alert("请刷新重试")
                              //                }
                              // });
                              // 
                              var btn = $(this);  
                                        var count = 60;  
                                        var resend = setInterval(function(){  
                                                   count--;  
                                                   if (count > 0){  
                                                       btn.val("重新获取 ("+count+") ").css({
                                                         'cursor':'not-allowed',
                                                         'background':'#CCC'
                                                       });   
                                                       $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});  
                                                   }else {  
                                                       clearInterval(resend);  
                                                       btn.val("获取验证码").removeAttr('disabled style').css("background","red");  
                                                   }  
                                               }, 1000);  
                                               btn.attr('disabled',true).css('cursor','not-allowed'); 
                            /*}*/
                           }
                        }
                   }
                 } 
        } 
      })
})
// 提交按钮
$(document).on("click","#submit",function(){
       var  phone=$("#userphone").val();
       var  password=$("#password").val();
       var  passagain=$("#passagain").val();
       var  zhizhaohao=$("#zhizhaohao").val();
       var  yzm=$("#yzm").val();
       
    if(phone=""||!(/^1[34578]\d{9}$/.test(phone))){
         $("#userphone").parents(".user-item").find(".tishi-msg").show().html("手机号码不正确")
    }else{
        if(password==""||!(/^[A-Za-z0-9]{6,20}$/.test(password))){
           $("#password").parents(".user-item").find(".tishi-msg").show().html("密码输入有误") 
        }else{
            if(passagain!=password){
               $("#password").parents(".user-item").find(".tishi-msg").show().html("两次输入密码不一致")  
            }else{
               if($('.user-title1').hasClass("borderred")){
                      if(yzm==""){
                         $("#yzm").parents(".user-item").find(".tishi-msg").show().html("验证码不能为空")  
                      }else{
                        $("#yzm").parents(".user-item").find(".tishi-msg").hide()
                        	
                        	var users={};
                        	users.phone=$("#userphone").val();
                        	users.password=password;
                        	users.yzm=yzm;
                        	
                          $.ajax({
                                         type:"post",
                                         url:"/SST/user/register.do",
                                         async:true,
                                         data: users,
                                         success: function(res){
                                            alert(res);
                                         },
                                         error: function(){
                                           alert("请刷新重试")
                                         }
                        });
                      }
                }else{
                    if (zhizhaohao=="") {
                       $("#zhizhaohao").parents(".user-item").find(".tishi-msg").show().html("营业执照号不能为空")  
                    }else{
                      /*if(!(/(^\d{15}$)|(^\d{18}$)/.test(zhizhaohao))) {
                            $(this).parents(".user-item").find(".tishi-msg").show().html("营业执照号格式错误")

                          }else{*/
                             $(this).parents(".user-item").find(".tishi-msg").hide()
                             if(yzm==""){
                                   $("#yzm").parents(".user-item").find(".tishi-msg").show().html("验证码不能为空")  
                             }else{
                                  $("#yzm").parents(".user-item").find(".tishi-msg").hide()
                                  	var users={};
		                        	users.phone=$("#userphone").val();
		                        	users.password=password;
		                        	users.yzm=yzm;
		                        	users.zhizhaohao;
		                        
                                  $.ajax({
                                         type:"post",
                                         url:"/SST/user/register1.do",
                                         async:true,
                                         data:users,
                                         success: function(res){
                                            alert(res);
                                         },
                                         error: function(){
                                           alert("请刷新重试")
                                         }
                                  });    

                           /*  }*/
                          }
                    }
                }
                
            } 
        }
    }


})

  